package com.lei.myBatis;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.lei.myBatis.dao.RuleDaoMapper;
import com.lei.myBatis.entity.RuleEntity;

public class RuleTest {
	private static SqlSessionFactory sessionFactory;
	private static final String loaction="com/lei/myBatis/myBatisConfig.xml";
	static{
		try {
			InputStream stream=Resources.getResourceAsStream(loaction);
			sessionFactory=new SqlSessionFactoryBuilder().build(stream);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	public static SqlSessionFactory getSession(){
		return sessionFactory;
	}
	@Test
	public void testGet(){
		SqlSession session=sessionFactory.openSession();
		RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
		RuleEntity rule=ruleDaoMapper.getRuleById(2);
		System.out.println(rule);
	}
	@Test
	public void testQueryList(){
		SqlSession session=sessionFactory.openSession();
		
		RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
		RuleEntity rule=new RuleEntity();
		rule.setRuleName("查");
		List<RuleEntity> ruleList=ruleDaoMapper.getRuleList(rule);
		for (RuleEntity u : ruleList) {
			System.out.println(u);
		}
	}
	@Test
	public void testAdd(){
		SqlSession session=sessionFactory.openSession();
		RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
		RuleEntity rule=new RuleEntity();
		rule.setParentRuleNo("parentRuleNo");
		rule.setRuleNo("ruleNo");
		rule.setRuleName("ruleName");
		try {
			ruleDaoMapper.addRule(rule);
			session.commit();
			System.out.println("id: "+rule.getId());
		} catch (Exception e) {
			System.out.println("提交事务发生异常: "+e.getMessage());
		} finally{
			session.close();
		}
	}
	@Test
	public void testUpdate(){
		SqlSession session=sessionFactory.openSession();
//		UserMapper userrMapper=session.getMapper(UserMapper.class);
//		User user=userrMapper.getUserById(4);
		RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
		RuleEntity rule=ruleDaoMapper.getRuleById(1);
		System.out.println("原先的User: "+rule);
		if (rule!=null) {
			rule.setParentRuleNo("parentRuleNo");
			rule.setRuleName("ruleName");
			rule.setRuleNo("ruleNo");
			int res=0;
			try {
				res=ruleDaoMapper.updateRule(rule);
				session.commit();
				System.out.println("提交成功: "+rule.getId()+" -- 操作返回结果: "+res);
			} catch (Exception e) {
				System.out.println("提交事务发生异常:"+e.getMessage());
			} finally{
				session.close();
			}
		}else{
			throw new RuntimeException("没有该用户!");
		}
	}
	
	@Test
	public void testDelete(){
		SqlSession session=sessionFactory.openSession();
		RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
		try {
			int res=ruleDaoMapper.deleteRule(5);		
			System.out.println("删除的执行结果:"+res);
			session.commit();
		} catch (Exception e) {
			System.out.println("提交事务发生异常: "+e.getMessage());
		} finally{
			session.close();
		}
	}
    @Test 
    public void listAllByTree() {
    	SqlSession session=sessionFactory.openSession();
    	RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
		List<RuleEntity> list = ruleDaoMapper.listAllByTree();
		System.out.println(list.size());
		System.out.println(list.get(0).getParentRuleNo());
		list.forEach(System.out::println);
    	
    }
}
